<?php

class Local
{
	var $fields;
	function Local()
	{
		$this->obj =& get_instance();
		$this->fields = array (
			array 
			(
				'label' => __("Squared thumbnail width", "album"),
				'input' => 'local_s_width',
				'info' => '',
				'default' => 75
			),
			array 
			(
				'label' => __("Small thumbnail width", "album"),
				'input' => 'local_t_width',
				'info' => '',
				'default' => 100
			),
			array 
			(
				'label' => __("Medium thumbnail width", "album"),
				'input' => 'local_m_width',
				'info' => '',
				'default' => 300
			),
			array 
			(
				'label' => __("View photo width", "album"),
				'input' => 'local_o_width',
				'info' => __("This is the image width when the thumbnail is clicked", "album"),
				'default' => 500
			)
			
		);
		$this->get_settings();
		
	}

	function store($image_data)
	{
		$result = array();
		//var_dump($image_data);

		$this->obj->load->plugin('upload');
	
		//resize to 100

		$this->upload = new upload($image_data['full_path']);

		
		$this->upload->image_resize = true;
		$this->upload->file_auto_rename = true;
		$this->upload->image_x = $this->album_settings->local_t_width;
		$this->upload->image_ratio_y = true;
		$this->upload->process('./media/images/t/');
		if($this->upload->processed)
		{
			$result['t'] = site_url('media/images/t/' . $this->upload->file_dst_name);
		}
		

		//creating thumbnail
		$this->upload->image_resize = true;
		$this->upload->file_auto_rename = true;
		$this->upload->image_x = $this->album_settings->local_s_width;
		$this->upload->image_y = $this->album_settings->local_s_width;
		$this->upload->image_ratio_crop = true ;
		$this->upload->process('./media/images/s/');
		if($this->upload->processed)
		{
			$result['s'] = site_url('media/images/s/' . $this->upload->file_dst_name);
		}

		//resize to 300

		$this->upload->image_resize = true;
		$this->upload->file_auto_rename = true;
		$this->upload->image_x = $this->album_settings->local_m_width;
		$this->upload->image_ratio_y = true;
		$this->upload->process('./media/images/m/');
		if($this->upload->processed)
		{
			$result['m'] = site_url('media/images/m/' . $this->upload->file_dst_name);
		}

		//resize to 500

		$this->upload->image_resize = true;
		$this->upload->file_auto_rename = true;
		$this->upload->image_x = $this->album_settings->local_o_width;
		$this->upload->image_ratio_y = true;
		$this->upload->process('./media/images/o/');
		if($this->upload->processed)
		{
			$result['o'] = site_url('media/images/o/' . $this->upload->file_dst_name);
		}

		
		if(isset($result['s']) && isset($result['t']) && isset($result['m']) && isset($result['o']))
		{
			@unlink($image_data['full_path']);
			return $result;
		}
		else
		{

			return false;
		}

	}

	function get_settings()
	{
		$query = $this->obj->db->get('album_settings');
		if ($query->num_rows() > 0)
		{
		   foreach ($query->result() as $row)
		   {
			  $this->album_settings->{$row->name} = $row->value;
		   }
		}
		//assign default fields if not set
		foreach($this->fields as $field)
		{
			if(!isset($this->album_settings->{$field['input']})) $this->album_settings->{$field['input']} = $field['default'];
		}	
		
	}
	
}

